<template>
  <div class="background">
    <div class="tbody">
      <div class="login">
        <br />
        <br />
        <h2 align="left" style="margin-left: 50px">用户登录</h2>
        <br />
        <div style="width: 350px">
          <el-form
            ref="form"
            :model="ruleForm"
            :rules="rules"
            label-width="80px"
            status-icon
          >
            <el-form-item label="账号" prop="username" label-width="100px">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" label-width="100px">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label-width="auto">
              <el-button type="primary" @click="submitForm">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="others"></div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
export default {
  components: {},
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 12,
            max: 12,
            message: "用户名为12字符学号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      let username = this.ruleForm.username;
      let password = this.ruleForm.password;
      let _this = this;
      $.ajax({
        type: "post",
        url: "http://localhost:8080/forum/login",
        data: { username, password },
        success: function (response) {
          console.log(response);
          if (response != "") {
            sessionStorage.setItem("user", JSON.stringify(response));
            //_this.$store.commit("setUserMutations", response);
            // console.log(_this.$store.state.user);
            _this.$message({
              message: "登录成功",
              type: "success",
              duration: 1000,
            });
            _this.$router.push("/Home/Index");
            _this.resetSetItem("login", true, true);
          } else {
            _this.$message({
              message: "密码错误，请重试",
              type: "error",
              duration: 1500,
            });
            _this.ruleForm.password = "";
          }
        },
      });
    },
  },
};
</script>

<style scoped>
.background {
  width: 1004px;
  overflow: auto;
  background-color: #f5f5f5;
  margin: 0 auto;
  min-height: 700px;
}

.login {
  margin-top: 80px;
  margin-left: 100px;
  margin-bottom: 10px;
  /* padding-top:30px; */
  width: 800px;
  height: 400px;
  background-color: #f8f8f8;
}

#uName,
#uPass {
  height: 30px;
  width: 220px;
  padding-left: 6px;
  font-size: 17px;
  border: 1px solid #9ecffe;
}

.butt {
  background-color: #6699cc;
  width: 80px;
  height: 30px;
  border: 0;
  color: white;
  font-size: 18px;
}

.butt:hover {
  background-color: #71aae3;
  border: 0;
}
</style>
